﻿using System.Collections.ObjectModel;
using System.ComponentModel.Composition;

namespace GaleSong.UI
{
    [PartCreationPolicy(CreationPolicy.Shared)]
    [Export(typeof(IWorkbench))]
    public class Workbench : IWorkbench
    {
        #region Field

        #endregion

        #region Property

        [Import]
        public IPageManager<IPage> Pages { get; set; }

        [Import]
        public IPadManager<IPad> Pads { get; set; }

        [Import]
        public IDialogManager<IDialog> Dialogs { get; set; }

        // Import Parts last due to it depend Page & Pad Commands.
        [Import]
        public IPartManager<IPart> Parts { get; set; }

        public ObservableCollection<Category> Categories
        {
            get { return new ObservableCollection<Category>() { new Category() { Pages = Pages } }; }
        }

        #endregion

        public T Get<T>(string name)
        {
            var type = typeof(T);
            if (typeof(IPart).IsAssignableFrom(type))
            {
                return (T)Parts[name];
            }
            else if (typeof(IPad).IsAssignableFrom(type))
            {
                return (T)Pads[name];
            }
            else if (typeof(IPage).IsAssignableFrom(type))
            {
                return (T)Pages[name];
            }
            else if (typeof(IDialog).IsAssignableFrom(type))
            {
                return (T)Dialogs[name];
            }
            return default(T);
        }
    }

    /// <summary>
    ///     The DevExpress Ribbon control only support Category binding, wrap the Pages inside.
    /// </summary>
    public class Category
    {
        public IPageManager<IPage> Pages { get; set; }
    }

    [Export(typeof(IPartManager<IPart>))]
    public class PartManager : PartManager<IPart, IPartMetaData>
    {
    }

    [Export(typeof(IPageManager<IPage>))]
    public class PageManager : PageManager<IPage, IPageMetaData>
    {
    }

    [Export(typeof(IPadManager<IPad>))]
    public class PadManager : PadManager<IPad, IPadMetaData>
    {
    }

    [Export(typeof(IDialogManager<IDialog>))]
    public class DialogManager : DialogManager<IDialog, IDialogMetaData>
    {
    }
}

